#include "Image.h"
#include <string>
#include "stb_image_write.h"
#include <ctime>

bool Image::Save(const std::string& dir, int w , int h) {
    if (!data) {
        return false;
    }

    w = (w == 0 ? this->width : w);
    h = (h == 0 ? this->height : h);

    time_t t = time(0);
    char tmpBuf[1024];
    strftime(tmpBuf, 1024, "%H-%M-%S", localtime(&t)); //format date  

    static char temp[1024] = { 0 };
    sprintf_s(temp, sizeof(temp), "%s/%s_%d_%d_%d_%d_%d_%d_%d.tga",  dir.c_str(), tmpBuf, id, w, h,width,height, format, type);

    if (type == GL_UNSIGNED_BYTE) {
        if (format == GL_RGBA)
        {
            return stbi_write_tga(temp, w, h, 4, data);
        }
        else if (format == GL_RGB) {
            return stbi_write_tga(temp, w, h, 3, data);
        }
        else if (format == GL_ALPHA) {

            return stbi_write_tga(temp, w, h, 1, data);
        }
        else {
            OutputDebugStringA(temp);
        }
    }
    OutputDebugStringA(temp);
    return true;
}
